class Solution(object):
    def reverseBetween(self, head, left, right):
        """
        :type head: ListNode
        :type left: int
        :type right: int
        :rtype: ListNode
        """
        if left == right or head == None or head.next == None:
            return head
        #找到left的pre和right的next
        dummyHead = ListNode()
        dummyHead.next = head
        
        #记录位置
        index = 1
        pre = None
        cur = head
        while cur != None and index != left:
            pre = cur
            cur = cur.next
            index += 1
        #保留前一个节点
        left_pre = pre
        left = cur
        #cur为left节点，开始反转left到right部分
        while cur != None and index != right+1:
            #保留下一个节点
            next = cur.next
            #反转
            cur.next = pre
            pre = cur
            cur = next
            index += 1
        #连接反转部分
		#易错，需要判断
        if left_pre is not None:
            left_pre.next = pre
        #left == 1
        else:
            dummyHead.next = pre
        left.next = cur
        return dummyHead.next
            

